package com.fmsz.xmly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.fmsz.xmly.R;
import com.fmsz.xmly.entitys.findanchor.AdvertiseEntity;
import com.fmsz.xmly.utils.Urls;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class AdvertiseActivity extends AppCompatActivity {
    private ImageView imageView;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            startActivity(new Intent(AdvertiseActivity.this,HomeActivity.class));
            finish();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advertise);

        init();
    }

    private void init() {
        handler.sendEmptyMessageDelayed(1,2000);
        imageView = (ImageView) findViewById(R.id.iv_advertise);

        RequestQueue requestQuene = Volley.newRequestQueue(this);
        StringRequest stringrequest = new StringRequest(Request.Method.GET, Urls.ADVERTISE, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray array = new JSONObject(response).getJSONArray("data");
                    List<AdvertiseEntity> list = JSON.parseArray(array.toString(), AdvertiseEntity.class);
                    int random= (int) (Math.random()*4-1);
                    Picasso.with(getApplicationContext()).load(list.get(random).getCover()).into(imageView);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },null);
        requestQuene.add(stringrequest);
    }
}
